package practice.adil.encrypordecryp.JavaEncryptionOne;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 * Created by 李园 - Adil on 2017/12/18.
 */
public class EncryptionRSA {
    /**
     * 加密
     * @param publicKey
     * @param srcBytes
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    protected byte[] Encrypt(RSAPublicKey publicKey, byte[] srcBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        if (publicKey != null) {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,publicKey);
            byte[] resultBytes = cipher.doFinal(srcBytes);
            return resultBytes;
        }
        return null;
    }

    /**
     * 解密
     * @param privateKey
     * @param srcBytes
     * @return
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    protected byte[] Decrypt(RSAPrivateKey privateKey,byte[] srcBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        if (privateKey != null) {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,privateKey);
            byte[] results = cipher.doFinal(srcBytes);
            return results;
        }

        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
        EncryptionRSA rsa  = new EncryptionRSA();
        String msg = "志强是傻吊";
        // KeyPairGenerator 类用于生成公钥和秘钥对，基于RSA算法生成对象
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        // 初始化密钥对生成器，密钥大小为1024位
        keyPairGenerator.initialize(1024);
        // 生成一个密钥对 ，保存在keyPair中
        KeyPair  keyPair = keyPairGenerator.generateKeyPair();
        // 得到秘钥
        RSAPrivateKey  privateKey = (RSAPrivateKey) keyPair.getPrivate();
        // 得到公钥
        RSAPublicKey  publicKey  = (RSAPublicKey) keyPair.getPublic();

        //使用公钥开始加密
        byte[] srcBytes  = msg.getBytes();
        byte[] enContext = rsa.Encrypt(publicKey, srcBytes);


        // 使用秘钥解密
        byte[] deContext = rsa.Decrypt(privateKey, enContext);

        System.out.println("明文是:" +msg);
        System.out.println("加密后：" + new String(enContext));
        System.out.println("解密后：" +new String(deContext));
    }

}
